package com.csw.shuanfa.CodeImprove.CompleteFutureLinkedBlockingQueue;

import com.csw.shuanfa.utils.ThreadPoolUtil;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

public class CallableTestShiZhan {
    public static void main(String[] args) throws InterruptedException,
            ExecutionException {
        int num = 10;
        ExecutorService threadPool = ThreadPoolUtil.getExecutorServiceCPU();

        try {
            long t1 = System.currentTimeMillis();
            AtomicInteger a = new AtomicInteger();
            Callable<Object> call = getObjectCallable(a, threadPool);
            //调用每个步骤
            Future<Object> future1 = threadPool.submit(call);
            Future<Object> future2 = threadPool.submit(call);
            Future<Object> future3 = threadPool.submit(call);


            Object obj1 = future1.get(3000 * 1, TimeUnit.MILLISECONDS);
            Object obj2 = future2.get(3000 * 1, TimeUnit.MILLISECONDS);
            Object obj3 = future3.get(3000 * 1, TimeUnit.MILLISECONDS);

            long t2 = System.currentTimeMillis();
            //t2 - t1);
            //a.get());
        } catch (TimeoutException ex) {
            //处理超时啦....");
            ex.printStackTrace();
        } catch (Exception e) {
            //处理失败.");
            e.printStackTrace();
        }
    }

    private static Callable<Object> getObjectCallable(AtomicInteger a, ExecutorService singleThreadPool) {
        Callable<Object> call = new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                //开始执行耗时操作
                Thread.sleep(2000);//每个任务的处理时间
                a.getAndIncrement();
                //Thread.currentThread().getName());
                return null;
            }
        };
        return call;
    }

}
